package util;

import java.util.List;

import model.FundDAO;

import org.mybeans.dao.DAOException;

import databean.FundBean;
import databean.FundPriceHistoryBean;
import databean.TransactionBean;

public class Bean2XML {
	public static String FundPriceHistorys(List<FundPriceHistoryBean> hs,FundDAO fundDAO) {
		String rs = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<historys>\n";
		for (FundPriceHistoryBean history : hs) {
			rs += FundPriceHistory(history, fundDAO);
		}
		rs += "</historys>";
		return rs;
	}

	private static String FundPriceHistory(FundPriceHistoryBean history,FundDAO fundDAO) {
		String rs = "<history>\n";
		
		try {
			
			rs += "<fund_name>" + fundDAO.lookup(history.getFundID()).getName() + "</fund_name>\n";
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		rs += "<price>" + history.getFormatedPrice() + "</price>\n";
		rs += "<date>" + history.getPriceDate().toString() + "</date>\n";
		rs += "</history>\n";
		return rs;
	}
	
	public static String Transactions(TransactionBean[] ts,FundDAO fundDAO) {
		String rs = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<transactions>\n";
		for (TransactionBean transaction : ts) {
			rs += Transaction(transaction,fundDAO);
		}
		rs += "</transactions>";
		return rs;
	}

	private static String Transaction(TransactionBean tran, FundDAO fundDAO) {
		String rs = "<transaction>\n";
		String status=null;
	
		if(tran.getExecuteDate()==null){
			status="Pending";
		}
		else{
			status="Complete";
		}
			
			
		rs += "<id>" + tran.getTransactionID() + "</id>\n";
		Boolean ifWhite=true;
		try {
			String name="";
			FundBean fund=fundDAO.lookup( tran.getFundID());
			if(fund!=null){
				ifWhite=false;
				name=fund.getSymbol();
			}
			rs += "<fund_name>" + name+ "</fund_name>\n";
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		if(ifWhite){
			rs += "<price>" + "</price>\n";
			rs += "<share>" + "</share>\n";
			rs += "<amount>$" + tran.getAmountFormatted() + "</amount>\n";			
		}
		else if(status.equalsIgnoreCase("Pending")){
			//case buy
			if(tran.getTransactionType().equalsIgnoreCase("buy")){
				rs += "<price>" + "</price>\n";
				rs += "<share>" + "</share>\n";
				rs += "<amount>$" + tran.getAmountFormatted() + "</amount>\n";
				
			}
			//case sell
			else if(tran.getTransactionType().equalsIgnoreCase("sell")){
				rs += "<price>" + "</price>\n";
				rs += "<share>" + tran.getFormatedShare()+"</share>\n";
				rs += "<amount>"  + "</amount>\n";
				
			}
		}
		else{
			rs += "<price>$" + tran.getFormatedSharedPrice() + "</price>\n";
			rs += "<share>" + tran.getFormatedShare() + "</share>\n";
			rs += "<amount>$" + tran.getAmountFormatted() + "</amount>\n";
			
		}
		rs += "<status>" + status + "</status>\n";
		rs += "<type>" + tran.getTransactionType() + "</type>\n";
		String date="";
		String color="FFCC33";
		if(status.equalsIgnoreCase("Complete")){
			date=tran.getExecuteDate().toString();
			color="92B901";
		}
		rs += "<date>" + date + "</date>\n";
		
		rs += "<color>" + color + "</color>\n";
		rs += "</transaction>\n";
		return rs;
	}
}
